package com.baolai.controller;

import com.alibaba.fastjson.JSONObject;
import com.baolai.entity.Goods;
import com.baolai.result.Result;
import com.baolai.result.ResultUtil;
import com.baolai.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "备件")
@ResponseBody
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @ApiOperation(value = "添加备件")
    @PostMapping("add")
    public Result addGoods(@RequestBody Goods goods){
        return goodsService.addGoods(goods);
    }

    @ApiOperation(value = "根据备件编码获取备件")
    @PostMapping("get")
    public Result getGoods(@RequestBody JSONObject jsonObject){
        return ResultUtil.success(goodsService.getById(jsonObject.getString("goodsId")));
    }

    @ApiOperation(value = "分页查询备件")
    @PostMapping("list")
    public Result listGoods(@RequestBody JSONObject jsonObject){
        return ResultUtil.success(goodsService.listGoods(jsonObject.getInteger("current"),jsonObject.getInteger("size")));
    }

    @ApiOperation(value = "根据备件名称或备件类型的关键字分页查询备件")
    @PostMapping("query")
    public Result queryGoods(@RequestBody JSONObject jsonObject){
        return ResultUtil.success(goodsService.queryGoods(jsonObject.getString("name"),jsonObject.getString("type"),jsonObject.getInteger("current"),jsonObject.getInteger("size")));
    }

    @ApiOperation(value = "修改备件信息")
    @PostMapping("update")
    public Result updateGoods(@RequestBody Goods goods){
        if(goodsService.updateGoods(goods)){
            return ResultUtil.success("修改成功");
        }
        return ResultUtil.error("修改失败");
    }
}
